package com.xjtu.controller;


import com.xjtu.netty.ChatHandler;
import com.xjtu.model.Chat;
import com.xjtu.service.ChatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;


@RestController
@Api(tags = "聊天")
@RequestMapping("/chat")
public class ChatController {
    @Resource
    private ChatService chatService;


    @ApiOperation("登录")
    @PostMapping("/login")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "username", value = "账号", required = true),
            @ApiImplicitParam(dataType = "String", name = "password", value = "密码", required = false),
    })
    public boolean getChatRecord(@RequestBody Map<String, String> map) {
        String username = map.get("username");
        String password = map.get("password");
        return chatService.login(username, password);
    }


    @ApiOperation("查看在线用户")
    @GetMapping("/online")
    public List<String> onlineList() {
        return ChatHandler.getOnlineList();
    }


    @ApiOperation("查看在线IP")
    @GetMapping("/ip")
    public Set<String> ipList() {
        return ChatHandler.IP_LIST;
    }


    @ApiOperation("获取聊天记录")
    @GetMapping("/get")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "id1", value = "用户1id", required = true),
            @ApiImplicitParam(dataType = "String", name = "id2", value = "用户2id", required = true),
            @ApiImplicitParam(dataType = "int", name = "limit", value = "条数限制", required = false)
    })
    public List<Chat> getChatRecord(@RequestParam("id1") String id1,
                                    @RequestParam("id2") String id2,
                                    @RequestParam(value = "limit", required = false) Integer limit) {
        return chatService.getChatRecord(id1, id2, limit);
    }


    @ApiOperation("获取全部聊天记录")
    @GetMapping("/getAll")
    @ApiImplicitParam(dataType = "String", name = "id", value = "用户id", required = true)
    public Map<String, List<Chat>> getChatRecord(@RequestParam("id") String id) {
        return chatService.getAllChatRecord(id);
    }

    @GetMapping("/add")
    public void addChatRecord(@RequestParam String text) {
        chatService.addChatRecord(text);
    }
}
